﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using ProjectHT.YKDGVB2012.Core.Datacontract;
using Telerik.Web.UI;


namespace ProjectHT.YKDGVB2012.Core.UserControls
{
    public class uc_DanhsachDT:System.Web.UI.UserControl
    {
        //protected Panel LoadGrid;        

        private IMainList _imain;
        public uc_DanhsachDT()
        {
            var services = new MainServices(SPContext.GetContext(Context));
            _imain = services;

        }
        protected RadListView RadListView1;
        protected RadListView Radlist1;
        protected override void OnInit(EventArgs e)
        {
            RadListView1.AllowPaging = true;
            RadListView1.NeedDataSource += new EventHandler<RadListViewNeedDataSourceEventArgs>(RadListView1_NeedDataSource);
            Radlist1.NeedDataSource += new EventHandler<RadListViewNeedDataSourceEventArgs>(Radlist1_NeedDataSource);
            base.OnInit(e);
        }

        protected void Radlist1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            Radlist1.DataSource = Data().Where(p=>p.Expires.Equals(true)).ToList();
        }

        protected void RadListView1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            RadListView1.DataSource = Data();

        }

        private List<Mainlist> Data()
        {
            var mainlist = _imain.GetListAll().Select(item => new Mainlist { Title = item.Title, ID = item.ID, CreatedDate = item["Created"].ToString(), Status = (bool)item[Common.MainList.FIELDSTATUS], Expires = (bool)item[Common.MainList.FIELDEXPIRE], Exdate = Convert.ToDateTime(item[Common.MainList.FIELDEXPIREDATE]) }).ToList();
            return mainlist.Where(p => p.Status.Equals(true)).OrderByDescending(p => p.CreatedDate).ToList();
        }

        protected void Page_Load(object sender,EventArgs e)
        {
           
        }

        protected string GetTitle(string id)
        {
            var str = string.Empty;
            if (!String.IsNullOrEmpty(id))
            {
                var mainlist = _imain.GetbyId(Convert.ToInt32(id));
                var attach = mainlist.Attachments;
                if (mainlist != null)
                {

                    str = "<a href='/Read/Page/Toanvan.aspx?ids=" + mainlist.ID + "'>" + mainlist.Title + "</a>";
                }

            }
            return str;
        }

        protected string ReadFull(string id)
        {
            var str = string.Empty;

            if (!String.IsNullOrEmpty(id))
            {
                var mainlist = _imain.GetbyId(Convert.ToInt32(id));                
                 if(mainlist!=null)
               {               
                        str = "<a href='/Read/Page/Toanvan.aspx?ids=" + mainlist.ID + "'>Đọc toàn văn</a>";
               }   
                
            }

            return str;
        }

        protected string LinkDown(string id)
        {
            var str = string.Empty;
            if (!String.IsNullOrEmpty(id))
            {
                var mainlist = _imain.GetbyId(Convert.ToInt32(id));
                var attach = mainlist.Attachments;
                var count = attach.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        str = "<a href='/lists/" + Common.MainList.MAINLIST + "/attachments/" + mainlist.ID + "/" + attach[i] + "'>Tải xuống</a>";
                    }
                }
            }

            return str;
        }

        protected string LinkGopy(string id)
        {
            var str = string.Empty;

            if (!String.IsNullOrEmpty(id))
            {
                var mainlist = _imain.GetbyId(Convert.ToInt32(id));
                if (mainlist != null)
                {
                    str = "<a href='/Pages/Comment/Gopy.aspx?ids=" + mainlist.ID + "'>xem góp ý</a>";
                }
            }
            return str;            
        }
    }
}
